package org.zjvis.datascience.common.util;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author AaronY
 * 非DC内部定义类的工具类
 * @version 1.0
 * @since 2022/3/25
 */
public class ClazzUtil {

    public static <T> String getClassName(Class<T> clazz){
        char[] chars = clazz.getSimpleName().toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }

    public static void main(String[] args) {
        Class[] clazzs = new Class[]{String.class, Integer.class, Double.class, Number.class, JSONArray.class, Map.class,
                List.class, Set.class, Void.class, Pair.class, cn.hutool.core.lang.Pair.class};

        for (Class obj: clazzs){
            System.out.println(ClazzUtil.getClassName(obj));
        }
    }

    public static <T> Object initDefaultValue(Class<T> clazz) {
        if (Number.class.isAssignableFrom(clazz) ) {
            return 0;
        }else if(clazz.equals(String.class)){
            return "empty";
        } else if (clazz.equals(JSONArray.class)) {
            return new JSONArray();
        } else if (clazz.equals(Map.class)) {
            return Maps.newHashMap();
        } else if (clazz.equals(List.class) ) {
            return Lists.newArrayList();
        } else if (clazz.equals(Set.class)){
            return Sets.newHashSet();
        }else if (clazz.equals(Void.class)) {
            return null;
        } else if (clazz.equals(Pair.class)) {
            return Pair.of(null, null);
        } else if (clazz.equals(cn.hutool.core.lang.Pair.class)){
            return new cn.hutool.core.lang.Pair<>(null, null);
        }else {
            try {
                return clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
